package com.nanrui.mutualinductor.ui.page;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.batman.baselibrary.base.BaseActivity;
import com.batman.baselibrary.db.RtBean;
import com.batman.baselibrary.db.RtBean_Table;
import com.batman.baselibrary.utils.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.nanrui.mutualinductor.R;
import com.nanrui.mutualinductor.ui.adapter.HishoryAdapter;
import com.raizlabs.android.dbflow.sql.language.SQLite;
import com.ui.widget.UINavigationView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class HistorySearchActivity extends BaseActivity {


    @BindView(R.id.uinv)
    UINavigationView mUinv;

    @BindView(R.id.list_view)
    RecyclerView mListView;


    @BindView(R.id.btn_search)
    Button btn_search;

    @BindView(R.id.et_content)
    EditText et_content;

    private List<RtBean> mList;
    private HishoryAdapter mHishoryAdapter;

    public static final String KEY_DATA = "KEY_DATA";


    @Override
    protected boolean isRegisterEventBus() {
        return false;
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_history_search;
    }

    @Override
    public void initViews() {

        mUinv.setNavigationTitle("数据搜索");

        mList = new ArrayList<>();
        mHishoryAdapter = new HishoryAdapter(mList);

        View em = LayoutInflater.from(mContext).inflate(R.layout.view_no_data, null);
        mHishoryAdapter.setEmptyView(em);

        mListView.setLayoutManager(new LinearLayoutManager(mContext));
        mListView.setAdapter(mHishoryAdapter);
        mHishoryAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                RtBean rtBean = mList.get(position);
                Intent intent = getIntent();
                intent.putExtra(KEY_DATA, rtBean);
                setResult(RESULT_OK, intent);
                finish();
            }
        });


        btn_search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String text = et_content.getText().toString().trim();
                if (TextUtils.isEmpty(text)) {

                    ToastUtils.showLong("输入不能为空");
                } else {

                    List<RtBean> rtBean = SQLite.select().from(RtBean.class).where(RtBean_Table.num.eq(text)).queryList();
                    mList.clear();
                    mList.addAll(rtBean);
                    mHishoryAdapter.notifyDataSetChanged();

                    //点击搜索的时候隐藏软键盘
//                    UIKeyboardHelper.hideKeyboard(et_content);
                }
            }
        });

    }

    @Override
    public void loadData(Bundle savedInstanceState) {

    }
}
